package com.ishare.common.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ishare.common.constant.GlobalReturnCode;
import lombok.Data;

import java.util.List;

/**
 * @author 李嘉宾
 */
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class VoData<T> {

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 总个数
     */
    private Long totalCount;

    /**
     * 总页数
     */
    private Integer totalPage;

    /**
     * 返回数据
     */
    private T data;


    public static VoData<Void> success() {
        VoData<Void> voData = new VoData<>();
        voData.setCode(GlobalReturnCode.SUCCESS.getCode());
        voData.setMsg(GlobalReturnCode.SUCCESS.getMsg());
        return voData;
    }

    public static <T> VoData<T> success(T data) {
        VoData<T> voData = new VoData<>();
        voData.setCode(GlobalReturnCode.SUCCESS.getCode());
        voData.setData(data);
        voData.setMsg(GlobalReturnCode.SUCCESS.getMsg());
        return voData;
    }

    public static <T> VoData<List<T>> success(PageVoWrap<T> pageVoWrap) {
        VoData<List<T>> voData = new VoData<>();
        voData.setCode(GlobalReturnCode.SUCCESS.getCode());
        voData.setData(pageVoWrap.getData());
        voData.setTotalCount(pageVoWrap.getTotalCount());
        voData.setTotalPage(pageVoWrap.getTotalPage());
        voData.setMsg(GlobalReturnCode.SUCCESS.getMsg());
        return voData;
    }

    public static <T> VoData<T> error(Integer code, String msg) {
        VoData<T> voData = new VoData<>();
        voData.setCode(code);
        voData.setData(null);
        voData.setMsg(msg);
        return voData;
    }

    public static <T> VoData<T> error(Integer code, String msg, T data) {
        VoData<T> voData = new VoData<>();
        voData.setCode(code);
        voData.setData(data);
        voData.setMsg(msg);
        return voData;
    }
}
